
#import "FAMMineDeficitViewController.h"
#import "FAMMineDeficitWeakView.h"
@interface FAMMineDeficitViewController ()
@property (weak, nonatomic) IBOutlet UIScrollView *FAMScrollView;
@property (weak, nonatomic) IBOutlet UISlider *FAMThisWeekSlider;
@property (weak, nonatomic) IBOutlet UISlider *FAMLostWeekSlider;
@property (nonatomic, strong)UIView *FAMTitleView;
@property (nonatomic, strong)UILabel *FAMTitleLabel;
@property (weak, nonatomic) IBOutlet UILabel *FAMDietLabel;
@property (weak, nonatomic) IBOutlet UILabel *FAMSportLabel;


@property (weak, nonatomic) IBOutlet UILabel *FAMBudgetLabel;

@property (weak, nonatomic) IBOutlet UILabel *FAMDietNumberLabel;
@property (weak, nonatomic) IBOutlet UILabel *FAMSportNumberLabel;

@property (weak, nonatomic) IBOutlet UILabel *FAMTrackLabel;

@property (weak, nonatomic) IBOutlet UIProgressView *FAMCarbsProgressView;
@property (weak, nonatomic) IBOutlet UILabel *FAMCarbsLabel;

@property (weak, nonatomic) IBOutlet UIProgressView *FAMFatProgressView;
@property (weak, nonatomic) IBOutlet UILabel *FAMFatLabel;

@property (weak, nonatomic) IBOutlet UIProgressView *FAMProteinProgressView;
@property (weak, nonatomic) IBOutlet UILabel *FAMProteinLabel;

@property (weak, nonatomic) IBOutlet UILabel *FAMThisWeakLabel;


@end

@implementation FAMMineDeficitViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSDate *currentDate = [NSDate date];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDate *firstDayOfCurrentWeek;
    [calendar rangeOfUnit:NSCalendarUnitWeekOfYear startDate:&firstDayOfCurrentWeek interval:nil forDate:currentDate];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyyMMdd"];
    NSString *begin_date = [dateFormatter stringFromDate:firstDayOfCurrentWeek];
    NSString *end_date = [FAMTool FAM_getYearWithMonthWithDay];
    NSDateFormatter *typeDateFormatter = [[NSDateFormatter alloc] init];
    [typeDateFormatter setDateFormat:@"MM.dd"];
    [self FAM_showUi:[typeDateFormatter stringFromDate:firstDayOfCurrentWeek]];
    [self FAM_loadData:[NSString stringWithFormat:@"%@/%@",begin_date,end_date]];
    
}
- (void)FAM_showUi:(NSString *)weakFirstDay{
    self.FAMTitleView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 160, 44)];
    self.navigationItem.titleView = self.FAMTitleView;
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftButton setImage:[UIImage imageNamed:@"mine_left_arrows"] forState:UIControlStateNormal];
    leftButton.userInteractionEnabled = NO;
    [self.FAMTitleView addSubview:leftButton];
    leftButton.sd_layout.centerYEqualToView(self.FAMTitleView).leftEqualToView(self.FAMTitleView).widthIs(15).heightIs(22);
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightButton setImage:[UIImage imageNamed:@"mine_right_arrows"] forState:UIControlStateNormal];
    rightButton.userInteractionEnabled = NO;
    [self.FAMTitleView addSubview:rightButton];
    rightButton.sd_layout.centerYEqualToView(self.FAMTitleView).rightEqualToView(self.FAMTitleView).widthIs(15).heightIs(22);
    
    NSString *nowDay = [FAMTool FAM_getMonthWithDay];
    self.FAMTitleLabel = [[UILabel alloc]init];
    self.FAMTitleLabel.textColor = kBlackColor;
    self.FAMTitleLabel.text = [NSString stringWithFormat:@"%@-%@",weakFirstDay,nowDay];
    self.FAMTitleLabel.font = [UIFont fontWithName:@"Helvetica Neue Bold" size:20];
    self.FAMTitleLabel.textAlignment = NSTextAlignmentCenter;
    [self.FAMTitleView addSubview:self.FAMTitleLabel];
    self.FAMTitleLabel.sd_layout.heightIs(25).leftSpaceToView(leftButton, 5).rightSpaceToView(rightButton,5).centerYEqualToView(self.FAMTitleView);
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(FAM_weak)];
    [self.FAMTitleView addGestureRecognizer:tap];
    self.FAMScrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    [self.FAMThisWeekSlider setThumbImage:[UIImage imageNamed:@"mine_slider_yellow"] forState:UIControlStateNormal];
    [self.FAMLostWeekSlider setThumbImage:[UIImage imageNamed:@"mine_slider_gray"] forState:UIControlStateNormal];
    self.FAMBudgetLabel.text = [FAMUserManger shareInstance].userModel.basic_meta;
}
- (void)FAM_loadData:(NSString *)date{
    WeakSelf(self);
    [self showHudInKeyWindowWithHint:nil];
    [FAMHttpTool FAM_GetFoodmateStatisticsWithData:date success:^(FAMFoodmateStatisticsModel * _Nonnull model, NSString * _Nonnull msg) {
        CGFloat fats = 0;
        CGFloat proteins = 0;
        CGFloat carbs = 0;
        CGFloat target_fats = 0;
        CGFloat target_proteins = 0;
        CGFloat target_carbs = 0;
        for (int i = 0; i<model.cateList.count; i++) {
            FAMFoodmateStatisticsModel *tempModel = ((FAMFoodmateStatisticsModel *)model.cateList[i]).cate;
            fats += tempModel.fats.floatValue;
            proteins += tempModel.proteins.floatValue;
            carbs += tempModel.carbs.floatValue;
            
            target_fats += tempModel.target_fats.floatValue;
            target_proteins += tempModel.target_proteins.floatValue;
            target_carbs += tempModel.target_carbs.floatValue;
        }
        CGFloat TotalCalories = 0;
        for (int i = 0; i < model.cateList.count; i++) {
            CGFloat calories = ((FAMFoodmateStatisticsModel *)model.cateList[i]).cate.calories.floatValue;
            TotalCalories += calories;
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [weakSelf hideHudKeyWindow];
            weakSelf.FAMDietLabel.text = [NSString stringWithFormat:@"%ld days",model.cateList.count];
            weakSelf.FAMSportLabel.text = [NSString stringWithFormat:@"%ld days",model.cateList.count];
            
            weakSelf.FAMDietNumberLabel.text = [NSString stringWithFormat:@"%0.1f",TotalCalories];
            weakSelf.FAMTrackLabel.text = [NSString stringWithFormat:@"%0.1f",[FAMUserManger shareInstance].userModel.basic_meta.floatValue - TotalCalories];
            weakSelf.FAMCarbsLabel.text = [NSString stringWithFormat:@"%0.1f/%0.1fg",target_carbs,carbs];
            weakSelf.FAMCarbsProgressView.progress = target_carbs/carbs;
            weakSelf.FAMFatLabel.text = [NSString stringWithFormat:@"%0.1f/%0.1fg",target_fats,fats];
            weakSelf.FAMFatProgressView.progress = target_fats/fats;
            weakSelf.FAMProteinLabel.text = [NSString stringWithFormat:@"%0.1f/%0.1fg",target_proteins,proteins];
            weakSelf.FAMProteinProgressView.progress = target_proteins/proteins;
            weakSelf.FAMThisWeakLabel.attributedText = [FAMTool FAM_AttributedString:[NSString stringWithFormat:@"%0.1f",TotalCalories] styleTwoString:@"Kcal" styleColorOne:kWhiteColor styleColorTwo:kCustom0xColor(0xbababa, 1.0) styleFontOne:14 styleFontTwo:10];
            weakSelf.FAMThisWeekSlider.value = TotalCalories/[FAMUserManger shareInstance].userModel.basic_meta.floatValue;
        });
    } failure:^(NSString * _Nonnull errorInfo) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [weakSelf hideHudKeyWindow];
            [weakSelf showHint:errorInfo];
        });
    }];
}
- (void)FAM_weak{
    FAMMineDeficitWeakView *deficitWeakView = [[FAMMineDeficitWeakView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    WeakSelf(self);
    deficitWeakView.block = ^(NSDate * _Nonnull beginDate, NSDate * _Nonnull endDate) {
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyyMMdd"];
        NSString *begin_date = [dateFormatter stringFromDate:beginDate];
        NSString *end_date = [dateFormatter stringFromDate:endDate];
        [weakSelf FAM_loadData:[NSString stringWithFormat:@"%@/%@",begin_date,end_date]];
        NSDateFormatter *typeDateFormatter = [[NSDateFormatter alloc] init];
        [typeDateFormatter setDateFormat:@"MM.dd"];
        weakSelf.FAMTitleLabel.text = [NSString stringWithFormat:@"%@-%@",[typeDateFormatter stringFromDate:beginDate],[typeDateFormatter stringFromDate:endDate]];
    };
    [self.view addSubview:deficitWeakView];
}

@end
